package com.cap.dreamcircle.View;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.AdCommentBean;
import com.cap.dreamcircle.Model.Bean.AdCommentEntity;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.IDreamModel;
import com.cap.dreamcircle.Model.Impl.DreamModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.View.Adapter.AdCommentsAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by liucxu on 2017/11/8.
 */

public class AdCommentsActivity extends BaseActivity {

    @BindView(R.id.layout_back)
    LinearLayout layout_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;


    private String adId;
    private IDreamModel model;
    private AdCommentsAdapter adapter;
    private List<AdCommentEntity> datas = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_ad_comments;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        model = new DreamModelImpl();
        recycle_view.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AdCommentsAdapter(this, datas);
        recycle_view.setAdapter(adapter);
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.GetInstance().finishActivity(AdCommentsActivity.this);
            }
        });
        tv_title.setText("评论");
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                queryAdComments();
            }
        });
    }

    @Override
    public void setupData() {
        adId = getIntent().getStringExtra("AdId");
        if (TextUtils.isEmpty(adId)) {
            App.GetInstance().finishActivity(this);
        }
        queryAdComments();
    }

    private void queryAdComments() {
        model.QueryAdComments(adId).subscribe(new CommonSubscriber<AdCommentBean>() {
            @Override
            public void onSuccess(AdCommentBean response) {
                swipe_refresh_layout.setRefreshing(false);
                hideProgress();
                adapter.setDatas(response.getCommentArray());
            }

            @Override
            public void onFailure(BaseBean response) {
                swipe_refresh_layout.setRefreshing(false);
                ToastUtils.showShortToast(AdCommentsActivity.this, response.getDesc());
                hideProgress();
            }
        });
    }
}
